const { ccclass, property } = cc._decorator;

@ccclass
export default class AssetsManager {
    static 图片: any = {};
    static Animations: any = {};
    static Prefabs: any = {};
    static Audios: any = {};
    static Levels: any[] = [];

    public static Load_Level() {
        return new Promise<void>(async resolve => {
            cc.assetManager.loadBundle('Level', (err, bundle) => {
                bundle.loadDir("", function (err, assets) {
                    for (const iterator of assets) {
                        // AssetsManager.Levels[iterator.name] = iterator;
                        AssetsManager.Levels.push(iterator.json);
                    }
                    // console.log("AssetsManager.Levels", AssetsManager.Levels);
                    resolve();
                });
            });
        });
    }

    public static Load_Sprite() {
        return new Promise<void>(async resolve => {
            cc.assetManager.loadBundle('Sprite', (err, bundle) => {
                bundle.loadDir("", function (err, assets) {
                    for (const iterator of assets) {
                        AssetsManager.图片[iterator.name] = iterator;
                    }
                    // console.log("AssetsManager.卡片图片", AssetsManager.卡片图片);
                    resolve();
                });
            });
        });
    }

    public static Load_Prefab() {
        return new Promise<void>(async resolve => {
            cc.assetManager.loadBundle('Prefab', (err, bundle) => {
                bundle.loadDir("", function (err, assets) {
                    for (const iterator of assets) {
                        AssetsManager.Prefabs[iterator.name] = iterator;
                    }
                    // console.log("AssetsManager.Prefabs", AssetsManager.Prefabs);
                    resolve();
                });
            });
        });
    }

    public static Load_Audio() {
        return new Promise<void>(async resolve => {
            cc.assetManager.loadBundle('Audio', (err, bundle) => {
                bundle.loadDir("", function (err, assets) {
                    for (const iterator of assets) {
                        AssetsManager.Audios[iterator.name] = iterator;
                    }
                    // console.log("AssetsManager.Audios", AssetsManager.Audios);
                    resolve();
                });
            });
        });
    }

    public static async Load_All() {
        let promise_Sprite = AssetsManager.Load_Sprite();
        let promise_Prefab = AssetsManager.Load_Prefab();
        let promise_Audio = AssetsManager.Load_Audio();
        let promise_Level = AssetsManager.Load_Level();

        // Promise.all([promise_Sprite, promise_Prefab]).then((values) => {
        //     console.log(values);
        // });

        await Promise.all([promise_Sprite, promise_Prefab, promise_Audio, promise_Level]);
        // await Promise.all([promise_Sprite, promise_Prefab, promise_Audio]);
    }

    public static Init(_callback: Function = null) {
        let count = 0;
        const total_count = 1;
        let complete = () => {
            if (count >= total_count) {
                if (_callback) {
                    _callback();
                }
            }
        }

        return new Promise<void>(async resolve => {
            cc.assetManager.loadBundle('Sprite', (err, bundle) => {
                // bundle.loadDir("icon", function (err, assets) {
                bundle.loadDir("", function (err, assets) {
                    for (const iterator of assets) {
                        AssetsManager.图片[iterator.name] = iterator;
                    }
                    // console.log("AssetsManager.卡片图片", AssetsManager.卡片图片);
                    resolve();
                });
            });
        });

        // cc.assetManager.loadBundle('Animation', (err, bundle) => {
        //     bundle.loadDir("", function (err, assets) {
        //         for (const iterator of assets) {
        //             AssetsManager.Animations[iterator.name] = iterator;
        //         }
        //         // console.log("AssetsManager.Animations", AssetsManager.Animations);
        //         count++;
        //         complete();
        //     });
        // });
    }
}

/** 调试 */
window["AssetsManager"] = AssetsManager;